package com.jielihaofeng.atguigu.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

/**
 * @author Johnnie Wind
 * @description ByteBuf使用2
 * @date 2021/11/26 17:26
 */
public class NettyByteBuf02 {
    public static void main(String[] args) {

        // 创建ByteBuf
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello,world!", CharsetUtil.UTF_8);

        // 使用相关的方法
        if (byteBuf.hasArray()) {
            // 取出数组
            byte[] content = byteBuf.array();

            // 将content转成字符串
            System.out.println(new String(content, CharsetUtil.UTF_8));

            System.out.println("byteBuf=" + byteBuf);
            System.out.println(byteBuf.arrayOffset());
            System.out.println(byteBuf.readerIndex());
            System.out.println(byteBuf.writerIndex());

            System.out.println(byteBuf.readByte());
            System.out.println(byteBuf.getByte(0));

            // 可读字节数
            System.out.println(byteBuf.readableBytes());

            // for 循环取出
            for (int i = 0; i < byteBuf.readableBytes(); i++) {
                System.out.println(byteBuf.getByte(i));
            }

            // 读取某个范围
            System.out.println(byteBuf.getCharSequence(4,6, CharsetUtil.UTF_8));
        }
    }
}
